package com.school.sports.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.school.sports.entity.SysDictType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 字典类型Mapper接口
 */
@Mapper
public interface SysDictTypeMapper extends BaseMapper<SysDictType> {

    /**
     * 根据字典类型查询字典类型信息
     * @param dictType 字典类型
     * @return 字典类型对象
     */
    @Select("SELECT * FROM sys_dict_type WHERE dict_type = #{dictType} AND status = 1 AND deleted = 0")
    SysDictType selectByDictType(@Param("dictType") String dictType);

    /**
     * 检查字典类型是否存在
     * @param dictType 字典类型
     * @param excludeId 排除的ID（用于更新时检查）
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_dict_type WHERE dict_type = #{dictType} AND deleted = 0 " +
            "AND (#{excludeId} IS NULL OR id != #{excludeId})")
    int countByDictType(@Param("dictType") String dictType, @Param("excludeId") Long excludeId);

    /**
     * 查询所有启用的字典类型
     * @return 字典类型列表
     */
    @Select("SELECT * FROM sys_dict_type WHERE status = 1 AND deleted = 0 ORDER BY created_at ASC")
    List<SysDictType> selectAllEnabledDictTypes();

    /**
     * 根据名称模糊查询字典类型
     * @param dictName 字典名称
     * @return 字典类型列表
     */
    @Select("SELECT * FROM sys_dict_type WHERE dict_name LIKE CONCAT('%', #{dictName}, '%') " +
            "AND status = 1 AND deleted = 0 ORDER BY created_at ASC")
    List<SysDictType> selectByDictNameLike(@Param("dictName") String dictName);
}